package com.sxt.service;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.sxt.entity.Book;

public class FileBookFinderImpl implements IBookFinder {

	@Override
	public List<Book> findAll() {
		List<Book> books = new ArrayList<Book>();
		BufferedReader br = null;
		FileReader fr = null;
		try {
			fr = new FileReader("file/books.txt");
			br = new BufferedReader(fr);
			String line = null;
			while ((line = br.readLine()) != null) {
				String[] bookInfo = line.split(",");
				books.add(new Book(bookInfo[0], bookInfo[1]));
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fr != null) {
					fr.close();
					fr = null;
				}
				if (br != null) {
					br.close();
					br = null;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return books;
	}
}
